const app = getApp()
const WXAPI = require("../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp: app.globalData.imgHttp,
    qrCodeUrl: '',
    info: {},
    code: '',
    noisPeople: true, //是否是该卡持有人
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    //微信客户端扫码支付
    var wxUrl = decodeURIComponent(options.q)
    if (wxUrl && wxUrl != 'undefined') {
      var num = wxUrl.indexOf("?")
      wxUrl = wxUrl.substring(num + 1); //
      wxUrl = wxUrl.split("code=");
      wxUrl = wxUrl[1]
      wxUrl = JSON.parse(wxUrl)
      this.data.qrCodeUrl =  wxUrl.param,
      this.getParCollectionQrCodeUrl()
    }
    if (options.param) {
      this.data.qrCodeUrl = options.param
    }
    var token = wx.getStorageSync('token')
    console.log("token是+++", token)
    if (token == '') {
      this.silentAuthorization()
    } else {
      this.getParCollectionQrCodeUrl()
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  back() {
    wx.reLaunch({
      url: '/pages/userMarket/userMarket'
    })
  },
  silentAuthorization() { //匿名登录
    let that = this
    wx.login({
      success: function (res) {
        if (res.code) {
          that.data.code = res.code
          console.log('获取code', that.data.code)
          var data = {
            code: that.data.code
          }
          WXAPI.silentAuthorization(data, (res) => {
            if (res.code === 200) {
              console.log(res.result)
              wx.setStorageSync('token', res.result.token)
              setTimeout(() => {
                that.getParCollectionQrCodeUrl()
              }, 500)
            } else {
              wx.showToast({
                title: res.message,
                icon: 'none'
              })
            }
          })
        } else {
          console.log('获取code失败', res.errMsg)
        }
      }
    })
  },
  //唤醒扫一扫功能
  getScancode: function () {
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        let parameter = res.result.split("code=")[1];
        console.log(parameter)
        let result = JSON.parse(parameter)

        console.log(result)
        console.log("扫一扫参数", result.param)
        console.log("扫一扫类型", result.type)
        if (result.type == 1) {
          wx.navigateTo({
            url: '/userPageA/pages/scan/information/information?param=' + result.param
          })
        } else if (result.type == 2) {
          wx.navigateTo({
            url: '/userPageA/pages/scan/receiving_assets/receiving_assets?param=' + result.param
          })
        } else if (result.type == 5) {
            this.data.qrCodeUrl = result.param,
            this.getParCollectionQrCodeUrl()
        } else {
          console.log('else')
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  //解析二维码confirmInfo
  getParCollectionQrCodeUrl() {
    var that = this
    var data = {
      url: this.data.qrCodeUrl,
      // url:'2qAKko9ltkj1sfhyd%2F2TN5sKXL8B2WG0L2dHaHt7l8giDgVYHNAl0gWGPAOXD2rr',
      // url: '2qAKko9ltkj1sfhyd%2F2TN06qiAYTDcom5oIFHGluweCei5fRcF%2BmQ2PLxjYqYmbX'
    }
    WXAPI.parseRefundQrCode(data, (res) => {
      const data = res.result
      console.log("解析二维码", res)
      if (res.code === 200) {
        this.setData({
          info: res.result,
          noisPeople: true,
        })
      } else {
        console.log(res.message)
        if (res.message == '不是该卡持有人') {
          this.setData({
            noisPeople: false
          })
          wx.showModal({
            title: '',
            content: '该卡不是您本人持有的，请和商家核实',
            showCancel: false,
            confirmColor: '#FF5800',
            confirmText: '重新扫码',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.getScancode()
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          })
        }
      }
    })
  },
  returnCard(e) { //确认退卡
    let id = e.currentTarget.dataset.id
    var data = {
      customerCardId: id
    }
    WXAPI.confirmRefundCard(data, (res) => {
      const data = res.result
      if (res.code === 200) {
        wx.showToast({
          title: '退卡成功',
          icon: 'none'
        })
        setTimeout(() => {
          this.getParCollectionQrCodeUrl()
        }, 1000)
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  refund(e) { //确认退款
    let id = e.currentTarget.dataset.id
    var data = {
      customerCardId: id
    }
    WXAPI.confirmRefund(data, (res) => {
      const data = res.result
      if (res.code === 200) {
        wx.showToast({
          title: '确认退款成功',
          icon: 'none'
        })
        setTimeout(() => {
          this.getParCollectionQrCodeUrl()
        }, 500)
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})